package com.xcalendar.service.rest.impl;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import com.xcalendar.database.dao.LocationDAO;
import com.xcalendar.database.dao.UserDAO;
import com.xcalendar.database.table.Location;
import com.xcalendar.database.table.User;

@Path("/AddLocation/")
public class AddLocationResource {
	@GET
	@Produces({ "application/json" })
	@Path("/userName={userName}&locationName={locationName}&latitude={latitude}&longitude={longitude}/")
	public String addEvent(@PathParam("userName") String userName, @PathParam("locationName") String locationName,  @PathParam("latitude") String latitude, 
			 @PathParam("longitude") String longitude) {
		
		User userTemp = new User();
		userTemp.setUserName(userName);
		UserDAO userDAO = new UserDAO();
		User user = userDAO.readUser(userTemp);
		if(user == null) return "User is error.";
		LocationDAO locationDAO = new LocationDAO();
		Location newLocation = new Location();
		newLocation.setLocationName(locationName);
		newLocation.setLatitude(Double.parseDouble(latitude));
		newLocation.setLongitude(Double.parseDouble(longitude));
		newLocation.setUserKey(user.getKey());
		locationDAO.addLocation(newLocation);
		return "ok";
		
	}
}
